<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class ShopReturnAddressesModel extends Model {

    protected $table ="shop_return_addresses";

    protected $pk = 'shop_return_address_id';

    private $cacheKey="shop:return:address:id:";

    private $cacheTime=5184000;



    /*
   * 获取total
   * */
    public function getTotal($where=[]){
        return Db::name('shop_return_addresses')->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name('shop_return_addresses')->field("shop_return_addresses_id")->where($where);

        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();

        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $this->getInfo($item['shop_return_addresses_id']);
            }
            return $return;
        }
        return [];
    }


    /*
   * 获取订单状态详情
   * */
    public function getInfo($id){
        $cacheKey = $this->cacheKey.$id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            return json_decode($info,true);
        }

        $info = Db::name('shop_return_addresses')->find($id);

        if($info){
            $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
            return $info;
        }
        return $info;
    }


    /*
     * 获取店铺发货地址数量
     * */
    public function getShopSendAddressesNum($shop_id){
        $where=[];
        $where['shop_id'] = $shop_id;
        $where['category'] = 1;
        return $this->getTotal($where);
    }


    /*
   * 获取店铺收货地址数量
   * */
    public function getShopReturnAddressesNum($shop_id){
        $where=[];
        $where['shop_id'] = $shop_id;
        $where['category'] = 2;
        return $this->getTotal($where);
    }

    /*
     * 删除店铺默认地址
     * */

    public function cancelShopDefaultAddress($shop_id,$category){
        $where=[];
        $where['shop_id'] = $shop_id;
        $where['category'] = $category;
        $where['is_default'] = 1;
        return $this->updateAll($where,['is_default'=>0]);
    }


    /*
   * 添加地址
   * */
    public function addOne($data){
        if(isset($data['shop_return_addresses_id'])){
            $shop_return_addresses_id = $data['shop_return_addresses_id'];
            unset($data['shop_return_addresses_id']);
            return Db::name('shop_return_addresses')->where("shop_return_addresses_id",$shop_return_addresses_id)->update($data);
        }else{
            return Db::name('shop_return_addresses')->insertGetId($data);
        }

    }



    /*
     * 删除操作
     * */
    public function delAll($where){
        return Db::name('shop_return_addresses')->where($where)->delete();
    }


    /*
  * 更新操作
  * */
    public function updateAll($where,$data){
       return Db::name('shop_return_addresses')->where($where)->update($data);
    }


}